package com.atguigu.servlet;

import com.atguigu.bean.Cart;
import com.atguigu.bean.Order;
import com.atguigu.bean.OrderItem;
import com.atguigu.bean.User;
import com.atguigu.service.OrderService;
import com.atguigu.service.impl.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @ Author:任留辉
 * @ Description:客户端订单管理
 * @ Date:Created in 10:18 2021/3/20 0020
 */
public class OrderClientServlet extends BaseServlet {
    private OrderService orderService = new OrderServiceImpl();

    /**
     * 处理结账功能
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void checkout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取session对象
        HttpSession session = request.getSession();
        //通过session获取共享的购物车和用户信息
        User user = (User) session.getAttribute("user");
        Cart cart = (Cart) session.getAttribute("cart");
        //调用service处理业务逻辑，实现结账的功能
        String orderId = orderService.checkout(cart, user);
        //将购物车信息从session中删除
        session.removeAttribute("cart");
        //将订单编号在session中共享
        session.setAttribute("orderId", orderId);
        //重定向到checkout.jsp
        response.sendRedirect(request.getContextPath() + "/pages/cart/checkout.jsp");
    }

    /**
     * 查询我的订单
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void getMyOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取session
        HttpSession session = request.getSession();
        //获取session中共享的用户信息
        User user = (User) session.getAttribute("user");
        //调用service处理业务逻辑，查询我的订单
        List<Order> list = orderService.getMyOrder(user.getId());
        //将我的订单信息在请求域request中共享
        request.setAttribute("list", list);
        //转发到order_client.jsp
        request.getRequestDispatcher("/pages/client/order_client.jsp").forward(request, response);
    }

    /**
     * 查看订单详情
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void getDetails(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数，即订单编号
        String orderId = request.getParameter("orderId");
        //调用service处理业务逻辑，查询订单所对应的订单详情
        List<OrderItem> list = orderService.getDetails(orderId);
        //将订单详情在请求域request中共享
        request.setAttribute("list", list);
        //转发到order_details.jsp
        request.getRequestDispatcher("/pages/client/order_details.jsp").forward(request, response);
    }

    /**
     * 收货，status：1-->2
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    protected void takeOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取请求参数，即订单编号
        String orderId = request.getParameter("orderId");
        //调用service处理业务逻辑，实现收货的功能
        orderService.updateStatus(orderId, 2);
        //重定向到来源页面
        response.sendRedirect(request.getHeader("referer"));
    }
}
